package com.wswy.wzcx.jpush;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import com.umeng.message.IUmengCallback;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack.ICallBack;
import com.umeng.message.common.inter.ITagManager.Result;
import com.umeng.message.tag.TagManager.TCallBack;
import com.umeng.message.tag.TagManager.TagListCallBack;
import com.wswy.wzcx.BuildConfig;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.android.agoo.huawei.HuaWeiRegister;
import org.android.agoo.oppo.OppoRegister;
import org.android.agoo.vivo.VivoRegister;
import org.android.agoo.xiaomi.MiPushRegistar;

class UmengPushImpl extends SimplePushOperator {

  private static final String TAG = "UmengPushImpl";

  private static final String ALIAS_TYPE_PLATFORM = "uc_platform";


  private static final String SP_NAME_KEY = "push_app_3";

  private static final String SP_KEY_ALIAS = "push_alias";
  private static final String SP_KEY_TAGS = "push_tags";

  private SharedPreferences pushSp=null;

  private boolean debugLog = false;

  @Override
  public void initPush(Context context,boolean debug) {

    debugLog = debug;
    PushAgent pushAgent = PushAgent.getInstance(context);

    pushAgent.setMuteDurationSeconds(1);
    pushAgent.setDisplayNotificationNumber(0);
    if (debug){
      pushAgent.setPushCheck(true);
    }

    pushSp = context.getSharedPreferences(SP_NAME_KEY,Context.MODE_PRIVATE);


    if (!TextUtils.isEmpty(BuildConfig.UMENG_PUSH_KEY)){

      pushAgent.register(new IUmengRegisterCallback() {
        @Override
        public void onSuccess(String s) {
          loge("SUCCESS umeng push register "+s);
        }

        @Override
        public void onFailure(String s, String s1) {
          loge("FAIL umeng push register "+s+"    s1 "+s1);

        }
      });
    }
    if (!TextUtils.isEmpty(BuildConfig.PUSH_XIAOMI_ID) && !TextUtils.isEmpty(BuildConfig.PUSH_XIAOMI_KEY)){
      MiPushRegistar.register(context, BuildConfig.PUSH_XIAOMI_ID, BuildConfig.PUSH_XIAOMI_KEY);
    }
    if (!TextUtils.isEmpty(BuildConfig.PUSH_HUAWEI_ID)){
      HuaWeiRegister.register((Application) context);
    }
    if (!TextUtils.isEmpty(BuildConfig.PUSH_OPPO_ID) && !TextUtils.isEmpty(BuildConfig.PUSH_OPPO_KEY)){
      OppoRegister.register(context,BuildConfig.PUSH_OPPO_ID,BuildConfig.PUSH_OPPO_KEY);
    }
    if (!TextUtils.isEmpty(BuildConfig.PUSH_VIVO_ID) && !TextUtils.isEmpty(BuildConfig.PUSH_VIVO_KEY)){
      VivoRegister.register(context);
    }
  }

  @Override
  public void setAlias(final Context context,final  String alias,final  OnPushOperatorCallback<String> callback) {

    ICallBack callBack = new ICallBack() {
      @Override
      public void onMessage(boolean isSuccess, String message) {

        loge("setAlias  --> "+isSuccess+"   "+message);

        if (isSuccess){

          pushSp.edit().putString(SP_KEY_ALIAS,alias).apply();

          if (callback != null){
            callback.onOperatorCallback(alias);
          }

        }else {

          if (callback != null){
            callback.onOperatorCallback(null);
          }

        }

        if (callback != null) {

          if (isSuccess) {

            callback.onOperatorCallback(alias);
          } else {
            callback.onOperatorCallback(null);
          }
        }
      }
    };

    PushAgent.getInstance(context).addAlias(alias, ALIAS_TYPE_PLATFORM, callBack);

  }

  @Override
  public void deleteAlias(final Context context, String alias,final  OnPushOperatorCallback<String> callback) {

    if (TextUtils.isEmpty(alias)){
      alias = pushSp.getString(SP_KEY_ALIAS,null);
    }

    if (TextUtils.isEmpty(alias)){
      return;
    }

    final String finalAlias = alias;
    PushAgent.getInstance(context).deleteAlias(alias, ALIAS_TYPE_PLATFORM, new ICallBack() {
      @Override
      public void onMessage(boolean isSuccess, String message) {

        loge("deleteAlias  --> "+finalAlias+"    "+isSuccess +"  "+message);
        if (isSuccess){

          pushSp.edit().remove(SP_KEY_ALIAS).apply();
          if (callback != null){
            callback.onOperatorCallback(finalAlias);
          }

        }else {
          if (callback != null){
            callback.onOperatorCallback(null);
          }
        }

      }
    });
  }

  @Override
  public void addTag(Context context,final  String tag,final  OnPushOperatorCallback<Set<String>> callback) {

    PushAgent.getInstance(context).getTagManager().addTags(new TCallBack() {
      @Override
      public void onMessage(boolean isSuccess, Result result) {

        loge("addTag  --> "+isSuccess +" "+tag);
        if (isSuccess){

          Set<String> spTags = getSpTags();
          if (spTags == null){
            spTags = new HashSet<>();
          }
          spTags.add(tag);

          pushSp.edit().putStringSet(SP_KEY_TAGS,spTags).apply();

          if (callback != null){
            callback.onOperatorCallback(spTags);
          }

        }else {
          if (callback != null){
            callback.onOperatorCallback(null);
          }
        }

      }
    },tag);

  }

  @Override
  public void setTags(final Context context,final  Set<String> tags,
      final OnPushOperatorCallback<Set<String>> callback) {

    cleanTags(context, new OnPushOperatorCallback<Boolean>() {
      @Override
      public void onOperatorCallback(Boolean aBoolean) {

        loge("setTags   1  cleanTags "+aBoolean);

        if (aBoolean){

          String[] tagsArray = new String[tags.size()];
          tagsArray = tags.toArray(tagsArray);

          PushAgent.getInstance(context).getTagManager().addTags(new TCallBack() {
            @Override
            public void onMessage(boolean isSuccess, Result result) {

              loge("setTags   2  "+isSuccess+"   "+tags);

              if (isSuccess){

                Set<String> spTags = getSpTags();
                if (spTags == null){
                  spTags = new HashSet<>();
                }
                spTags.addAll(tags);

                pushSp.edit().putStringSet(SP_KEY_TAGS,spTags).apply();

                if (callback != null){
                  callback.onOperatorCallback(spTags);
                }

              }else {
                if (callback != null){
                  callback.onOperatorCallback(null);
                }
              }

            }
          },tagsArray);

        }else {

          Log.e(TAG, "setTags clean error --> !!!");

        }

      }
    });

  }

  @Override
  public void deleteTags(final Context context,final  Set<String> tags,
      final OnPushOperatorCallback<Set<String>> callback) {

    if (tags == null || tags.isEmpty()){
      return;
    }
    String[] tagsArray = new String[tags.size()];
    tagsArray = tags.toArray(tagsArray);

    PushAgent.getInstance(context).getTagManager().deleteTags(new TCallBack() {
      @Override
      public void onMessage(boolean isSuccess, Result result) {

        loge("deleteTags --> "+isSuccess+"   "+tags);

          if (isSuccess){

            Set<String> spTags = getSpTags();
            if (spTags == null){
              spTags = new HashSet<>();
            }
            spTags.removeAll(tags);
            pushSp.edit().putStringSet(SP_KEY_TAGS,spTags).apply();


            if (callback != null){
              callback.onOperatorCallback(tags);
            }

          }else {
            if (callback != null){
              callback.onOperatorCallback(null);
            }
          }


      }
    },tagsArray);
  }

  @Override
  public void cleanTags(final Context context,final  OnPushOperatorCallback<Boolean> callback) {

    Set<String> stringSet = getSpTags();

    loge("cleanTags --> 1 "+stringSet);
    if (stringSet != null && !stringSet.isEmpty()){

      String[] tagsArray = new String[stringSet.size()];
      tagsArray = stringSet.toArray(tagsArray);


      PushAgent.getInstance(context).getTagManager().deleteTags(new TCallBack() {
        @Override
        public void onMessage(boolean isSuccess, Result result) {

          loge("cleanTags -->2 "+isSuccess);

          if (isSuccess){

            pushSp.edit().remove(SP_KEY_TAGS).apply();

          }
          if (callback != null){
            callback.onOperatorCallback(isSuccess);
          }
        }
      },tagsArray);

    }else {
      if (callback != null){
        callback.onOperatorCallback(true);
      }
    }

  }

  @Override
  public void getTags(Context context,final  OnPushOperatorCallback<Set<String>> callback) {

    PushAgent.getInstance(context).getTagManager().getTags(new TagListCallBack() {
      @Override
      public void onMessage(boolean isSuccess, List<String> list) {

        loge("getTags -> "+isSuccess+"  "+list);

        if (isSuccess){
          Set<String> set =new HashSet<>();

          if (list != null && !list.isEmpty()){
            set.addAll(list);
          }
          if (!set.isEmpty()){
            pushSp.edit().putStringSet(SP_KEY_TAGS,set).apply();
          }else {
            pushSp.edit().remove(SP_KEY_TAGS).apply();
          }


          if (callback != null){

            callback.onOperatorCallback(set);
          }

        }else {
          if (callback != null){
            callback.onOperatorCallback(null);
          }
        }

      }
    });

  }

  @Override
  public void setMobileNumber(Context context, String mobileNumber,
      OnPushOperatorCallback<String> callback) {

  }

  @Override
  public void switchPush(Context context, boolean status) {

  }

  @Override
  public void stopPush(Context context) {
    PushAgent.getInstance(context).disable(new IUmengCallback() {
      @Override
      public void onSuccess() {
        loge("onSuccess --> stopPush");
      }

      @Override
      public void onFailure(String s, String s1) {
        loge( "onFailure --> stopPush "+s+"   "+s1);
      }
    });
  }

  @Override
  public void resumePush(Context context) {
    PushAgent.getInstance(context).enable(new IUmengCallback() {
      @Override
      public void onSuccess() {
        loge("onSuccess --> resumePush");
      }

      @Override
      public void onFailure(String s, String s1) {

        loge("onFailure --> resumePush "+s+"   "+s1);

      }
    });
  }

  private Set<String> getSpTags(){
    Set<String> stringSet = pushSp.getStringSet(SP_KEY_TAGS, null);
    if (stringSet != null && !stringSet.isEmpty()){
      Iterator<String> iterator = stringSet.iterator();
      while (iterator.hasNext()){
        if (TextUtils.isEmpty(iterator.next())){
          iterator.remove();
        }
      }
      if (!stringSet.isEmpty()){
        return stringSet;
      }
    }
    return null;
  }

  private void loge(String log){
    if (debugLog){
      Log.e(TAG, log);
    }
  }

}
